import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.Lexer;
import org.antlr.runtime.Token;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeAdaptor;
import org.antlr.runtime.tree.Tree;
import org.antlr.runtime.tree.TreeAdaptor;

public class ASTPrinter {

    public static void main(String[] args) throws Exception {
        new ASTPrinter().init();
    }

    public void init() throws Exception {
        // Read the source

        CharStream c = new ANTLRFileStream(
            "/Users/sam/Documents/workspace/AntlrJavaParser/src/main/java/Class1.java");

        // create the lexer attached to stdin
        Lexer lexer = new JavaLexer(c);
        // create the buffer of tokens between the lexer and parser
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        
        // create the parser attached to the token buffer
        // and tell it which debug event listener to use
        JavaParser parser = new JavaParser(tokens);

        // launch the parser using the treeadaptor
        parser.setTreeAdaptor(adaptor);
        
//        CommonTree typeParameterTree =  (CommonTree) parser.typeParameter().getTree();
//		System.out.println("Type Paramers" +parser.typeParameter().getTree());

        // Get the compilation unit item
		// CommonTree tree = (CommonTree) parser.importDeclaration().getTree();

		 JavaParser.compilationUnit_return ret = parser.compilationUnit();

        // Get the associated tree
        CommonTree tree = (CommonTree) ret.getTree();
       // System.out.println(tree.toStringTree());
        // Print the tree
        printTree(tree, 1);
        
        
       // parser.get
         
         
         
    }

    static final TreeAdaptor adaptor = new CommonTreeAdaptor() {
        public Object create(Token payload) {
            return new CommonTree(payload);
        }
    };
    
    
    public void printTree(CommonTree t, int indent) {
        System.out.println(t.toString());
        printTreeHelper(t, indent);
    }

    private void printTreeHelper(CommonTree t, int indent) {
        if (t != null) {
            StringBuffer sb = new StringBuffer(indent);
            for (int i = 0; i < indent; i++)
                sb = sb.append("   ");
            for (int i = 0; i < t.getChildCount(); i++) {
                //if (t.getChild(i).getType()==4)
                System.out.println(sb.toString() + t.getChild(i).toString()
                + " [" + JavaParser.tokenNames[t.getChild(i).getType()]
                + "]");
                printTreeHelper((CommonTree) t.getChild(i), indent + 1);
            }
        }
    }


//    public void printTree(CommonTree t, int indent) {
//        System.out.println(t.toString());
//        Map<String, String>tokens = printTreeHelper(t, indent);
//        
//        Set<String> identifiers =  new HashSet<String>();
//        Map<String,String> nonidentifiers =  new HashMap<String,String>();
//        for(String word: tokens.keySet()){
//        	if(tokens.get(word).equalsIgnoreCase("IDENTIFIER")){
//        		identifiers.add(word);
//        	} else {
//        		nonidentifiers.put(word,tokens.get(word));
//        	}
//        }
//        
//        System.out.println("IDENTIFIERS ----------------");
//        for(String word: identifiers){
//        	System.out.println(word);
//        }
//        System.out.println("NON - IDENTIFIERS ----------------");
//        for(String key: nonidentifiers.keySet()){
//        	System.out.println(key + "->" + nonidentifiers.get(key));
//        }
//        
//    }
//
//    private Map<String, String> printTreeHelper(CommonTree t, int indent) {
//    	Map<String,String> tokens =  new HashMap<String,String>();
//        if (t != null) {
//        	
//        	
//            StringBuffer sb = new StringBuffer(indent);
//            for (int i = 0; i < indent; i++)
//                sb = sb.append("   ");
//            for (int i = 0; i < t.getChildCount(); i++) {
//            	Tree child = t.getChild(i);
//            	System.out.println(child.getType());
//                //if (t.getChild(i).getType()==4)
//                System.out.println(sb.toString() + t.getChild(i).toString()
//                + " [" + JavaParser.tokenNames[t.getChild(i).getType()]
//                + "]");
//                tokens.put(t.getChild(i).toString(), JavaParser.tokenNames[t.getChild(i).getType()]);
//                printTreeHelper((CommonTree) t.getChild(i), indent + 1);
//            }
//        }
//        return tokens;
//    }
}